package com.bxait.toymail.product.service.impl;

import com.bxait.com.utils.PageUtils;
import com.bxait.com.utils.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bxait.toymail.product.dao.SpuImagesDao;
import com.bxait.toymail.product.entity.SpuImagesEntity;
import com.bxait.toymail.product.service.SpuImagesService;


@Service("spuImagesService")
public class SpuImagesServiceImpl extends ServiceImpl<SpuImagesDao, SpuImagesEntity> implements SpuImagesService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuImagesEntity> page = this.page(
                new Query<SpuImagesEntity>().getPage(params),
                new QueryWrapper<SpuImagesEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * @param id
     * @param images
     * @==============History===============<br/>
     * @Description // TODO  保存商品的spu图片集
     * @Date 2023/3/20 21:55
     * @Author <a href="qq:2277411582@qq.com">chen</a>
     * @return void
     */
    @Override
    public void saveImages(Long id, List<String> images) {
        if(images != null && images.size() > 0) {
            List<SpuImagesEntity> collect = images.stream().map(item -> {
                SpuImagesEntity spuImagesEntity = new SpuImagesEntity();
                spuImagesEntity.setSpuId(id);
                spuImagesEntity.setImgUrl(item);

                spuImagesEntity.setImgSort(0);
                spuImagesEntity.setImgName(item.substring(item.lastIndexOf("/") + 1));
                return spuImagesEntity;
            }).collect(Collectors.toList());
            this.saveBatch(collect);
        }

    }

}